package com.uuj.dome.validator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * ClassName:FlagValidatorClass
 * PACKAGE_NAME:com.uuj.dome.validator
 */
public class FlagValidatorClass implements ConstraintValidator<FlagValidator, Object> {
    /**
     * 临时变量保存flag值列表
     */
    private String values;

    @Override
    public void initialize(FlagValidator constraintAnnotation) {
        this.values = constraintAnnotation.value();
    }

    @Override
    public boolean isValid(Object o, ConstraintValidatorContext constraintValidatorContext) {
        // 分隔定义的有效值
        String[] value_arr = values.split(",");
        //
        boolean isFlag = false;
        // 遍历对比有效值
        for (int i = 0; i < value_arr.length; i++) {
            // 存在一致跳出循环，赋值isflag=true
            if (value_arr[i].equals(o)) {
                isFlag = true;
                break;
            }
        }
        // 返回是否存在的boolean
        return isFlag;
    }
}
